//
//  UserDetailVC.m
//  TheProjectName
//
//  Created by 李劲成 on 16/5/21.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "FriendDetailVC.h"
#import "UserDetailCell.h"
#import "ChatVC.h"
@interface FriendDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *m_tableView;

@property (nonatomic, retain) UIButton *send;

@property (nonatomic, retain) UIButton *delete;

@property (nonatomic, retain) NSArray *m_titleArray;

@property (nonatomic, retain) NSArray *m_infoArray;

@end

@implementation FriendDetailVC

- (instancetype)init{
    self = [super init];
    if (self) {
        self.m_titleArray = @[@"个性签名",@"地区", @"照片墙"];

        self.m_infoArray = @[@"无", @"无", @"无"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self config];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
  
}
- (void)config {
    self.navigationItem.title = @"个人信息";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style: 0 target:self action:@selector(handleBack)];
  
}
- (void)handleBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createSubViews {
    // tableView
    self.m_tableView = [[UITableView alloc]init];
    [self.view addSubview:_m_tableView];
    [_m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.height.mas_equalTo(SCREEN_HEIGHT - 200 - 64);
    }];
    _m_tableView.delegate = self;
    _m_tableView.dataSource = self;
    _m_tableView.bounces = NO;
    _m_tableView.backgroundColor = [UIColor clearColor];
    _m_tableView.separatorColor = [UIColor colorWithRed:0.90 green:0.90 blue:0.90 alpha:1.0];
    _m_tableView.rowHeight = (SCREEN_HEIGHT - 240 - 64) / (_m_titleArray.count + 1);
    [_m_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Header"];
    [_m_tableView registerClass:[UserDetailCell class] forCellReuseIdentifier:@"UserDetail"];
    
    // 发送消息按钮
    self.send = [UIButton buttonWithType:UIButtonTypeCustom];
    _send.layer.cornerRadius = 10;
    _send.showsTouchWhenHighlighted = YES;
    [_send setAttributedTitle:[[NSAttributedString alloc]initWithString:@"发送消息" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor whiteColor]} ] forState:UIControlStateNormal];
    [_send setAttributedTitle:[[NSAttributedString alloc]initWithString:@"发送消息" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor grayColor]} ] forState:UIControlStateHighlighted];
    [_send setBackgroundColor:[UIColor colorWithRed:0.29 green:0.75 blue:0.17 alpha:1.0]];
    [_send addTarget:self action:@selector(pushChat) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_send];
    [_send mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_m_tableView.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    // 删除
    self.delete = [UIButton buttonWithType:UIButtonTypeCustom];
    _delete.showsTouchWhenHighlighted = YES;
    _delete.layer.cornerRadius = 10;
    [_delete setAttributedTitle:[[NSAttributedString alloc]initWithString:@"删除好友" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor whiteColor]} ] forState:UIControlStateNormal];
    [_delete setAttributedTitle:[[NSAttributedString alloc]initWithString:@"删除好友" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor grayColor]} ] forState:UIControlStateHighlighted];
    [_delete setBackgroundColor: [UIColor redColor]];
    [_delete addTarget:self action:@selector(deleteFriend) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_delete];
    [_delete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_send.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(50);
    }];
    
}

#pragma mark - 删除好友
- (void)deleteFriend{
    // 删除好友
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除好友" message:[NSString stringWithFormat:@"请问你是否删除%@", _name] preferredStyle: UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        EMError *error = [[EMClient sharedClient].contactManager deleteContact:_name];
        if (!error) {
            NSLog(@"删除成功");
        }
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
        [self.navigationController popViewControllerAnimated:YES];
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
    }]];
    [self presentViewController:alert animated:YES completion:^{
    }];
    
}

- (void)pushChat {
    ChatVC *chatController = [[ChatVC alloc] initWithConversationChatter:_name conversationType:EMConversationTypeChat];
    chatController.title = _name;
    [self.navigationController pushViewController:chatController animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return _m_titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UserDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserDetail"];
    if (indexPath.section == 0) {
        [cell setTitle:@"用户" AndInfo:_name];
    }
    else{
    [cell setTitle:_m_titleArray[indexPath.row] AndInfo:_m_infoArray[indexPath.row]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType =    UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *head = [[UIView alloc]init];
    head.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"clear"]];
    
    return head;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
